/**
 * Created by JetBrains PhpStorm.
 * User: hoangnm
 * Date: 7/19/13
 * Time: 2:35 PM
 */
var slideInterval;
var arrHeight = [];
var skipAutoActiveMenu = false;
var balancePostion;
var getExtentChefWidth;

var delaySomethings = (function() {
    var timeDelay = 0;
    return function(callback, ms) {
        clearTimeout(timeDelay);
        timeDelay = setTimeout(callback, ms);
    }
})();
function widthForResSlide() {
    var w = $(window).width();
    var t = $("#sys_module_restaurant").children(".grid_frame").width();
    var t1 = $("#sys_res_left_content").width();
    var a = (w + t) / 2 - t1;
    $("#sys_wrap_img_slide").width(a);
}
function widthForIntro() {
    var w = $(window).width();
    var t = $("#sys_module_slide_people").children(".grid_frame").width();
    var t2 = $("#sys_right_slide_people").width();
    var a = (w + t) / 2 - t2;
    $("#sys_intro_people").width(a);
}
function widthForISlideExtent() {
    var sys_food_item = $(".sys_food_item");
    var grid_frame = $(".grid_frame").first();
    sys_food_item.each(function(idx,elem) {
        $(elem).find(".extent-menu").css({
            "width": $(window).width(),
            "left": "-" + $(elem).offset().left +"px"
        });
    });
}
function slideRestaurant(pageNumber) {
    var sys_wrap_img_slide = $("#sys_wrap_img_slide");
    var sys_res_information_slide = $("#sys_res_information_slide");
    var sys_pagination_slide_res = $("#sys_pagination_slide_res");
    var indexElemDOM = pageNumber - 1;
    if (pageNumber <= sys_wrap_img_slide.children().length) {
        sys_wrap_img_slide.children(".active").fadeOut(300, function () {
            sys_wrap_img_slide.children().removeClass("active").eq(indexElemDOM).fadeIn(300, function () {
                $(this).addClass("active");
            });
        });
        sys_res_information_slide.children().removeClass("active").eq(indexElemDOM).addClass("active");
        sys_pagination_slide_res.children().removeClass("active").eq(indexElemDOM).addClass("active");
        return true;
    } else {
        return "pageNumber:" + pageNumber + " invalid.";
    }
}
window.onload=function(){
    var getHash = location.hash;
    var sys_main_nav = $("#main-nav");
    if(getHash!="") {
        getHash = getHash.replace("#", "");
        $('html, body').stop(true,false).animate({
            scrollTop: $("#l-" + getHash).offset().top - $("#sys_module_header").height()
         }, 1000);
        var linkMenu=sys_main_nav.find("a[href=#"+getHash+"]").first();
        linkMenu.parent().addClass("active").siblings().removeClass("active");
        sys_main_nav.children(".mark").stop(true,false).animate({
            "left": (linkMenu.parent().position().left + 20) + "px"
        },700);

    }
    sys_main_nav.children("li").each(function(idx,val) {
        if(!$(val).hasClass("mark")){
            var getTarget = $(val).children("a").first().attr("href").replace("#", "");
            arrHeight.push($("#l-" + getTarget).offset().top);
        }
    });
};

$(function () {
    widthForResSlide();
    widthForIntro();
    var main_nav = $("#main-nav");
    var sys_module_header = $("#sys_module_header");

    $(window).resize(function () {
        widthForResSlide();
        widthForIntro();
        widthForISlideExtent();
    });
    $(window).scroll(function(){
        var scrY = $(window).scrollTop();
        var scrolled = scrY + sys_module_header.height();
        if (scrY > 10) {
            sys_module_header.addClass("active-shadow");
        }else{
            sys_module_header.removeClass("active-shadow");
        }
        //console.log("scrolled:" + (scrY + sys_module_header.height()));
        delaySomethings(function(){
            if(!skipAutoActiveMenu){
                scrY = $(window).scrollTop();
                scrolled = scrY + sys_module_header.height();
                var getIdxActive;
                for(var i=0;i<arrHeight.length;i++) {
                    if((scrolled + 10) < arrHeight[i]){
                        getIdxActive = i-1;
                        break;
                    }
                }
                var liActive = main_nav.children(":eq(" + getIdxActive + ")");
                liActive.addClass("active").siblings().removeClass("active");
                main_nav.children(".mark").stop(true,false).animate({
                    "left": (liActive.position().left + 20) + "px"
                },300);

            }else{
                skipAutoActiveMenu = false;
            }
        },100);
    });

    main_nav.on("click","a",function(){
        $(this).parent().addClass("active").siblings().removeClass("active");
        main_nav.children(".mark").stop(true,false).animate({
            "left": ($(this).parent().position().left + 20) + "px"
        },300);
        var getTarget = $(this).attr("href").replace("#", "");
        // Neu la menu Lien he thi chay ham on scroll cua window
        skipAutoActiveMenu = $(this).parents("li").hasClass("last_item");

        $('html, body').stop(true,false).animate({
            scrollTop: $("#l-" + getTarget).offset().top - $("#sys_module_header").height()
         }, 1000);
    });

    //Banner slide
    var sys_list_img_slide = $("#sys_list_img_slide");
    setInterval(function () {
        var indexIMG = sys_list_img_slide.children(".active").index();
        var nextIdx;
        if (indexIMG == sys_list_img_slide.children().length - 1) {
            nextIdx =0;
        }else{
            nextIdx = indexIMG + 1;
        }
        sys_list_img_slide.children(".active").fadeOut(300, function () {
            sys_list_img_slide.children().removeClass("active").eq(nextIdx).fadeIn(300, function () {
                $(this).addClass("active");
            });
        });
    }, 5000);

    var sys_pagination_slide_res = $("#sys_pagination_slide_res");
    var sys_res_information_slide = $("#sys_res_information_slide");
    sys_pagination_slide_res.on("click", "i", function () {
        var getIdx = $(this).index();
        slideRestaurant(getIdx + 1);
        clearInterval(slideInterval);
        slideInterval = setInterval(function () {
            var getIdx = sys_pagination_slide_res.children(".active").index();
            var currentPage = getIdx + 1;
            var nextPage;
            if (currentPage == sys_pagination_slide_res.children().length)
                nextPage = 1;
            else
                nextPage = currentPage + 1;
            slideRestaurant(nextPage);
        }, 5000);
    });
    sys_res_information_slide.on("click",".sys_link_map",function(){
        sys_pagination_slide_res.children().last().trigger("click");
    });
    slideInterval = setInterval(function () {
        var getIdx = sys_pagination_slide_res.children(".active").index();
        var currentPage = getIdx + 1;
        var nextPage;
        if (currentPage == sys_pagination_slide_res.children().length)
            nextPage = 1;
        else
            nextPage = currentPage + 1;
        slideRestaurant(nextPage);
    }, 5000);

    /*Slide đầu bếp*/
    var sys_overview_slide_people = $("#sys_overview_slide_people");
    var sys_btn_close_extent = $("#sys_btn_close_extent");
    var sys_wrap_btn_nav = $("#sys_wrap_btn_nav");
    var firstItemOffsetLeft = sys_overview_slide_people.offset().left;
    if (sys_overview_slide_people.children().length <= 4) {
        $("#sys_wrap_btn_nav").hide();
    }
    $('#slider-code').tinycarousel({
        initSuccess: function() {}
    });
    sys_overview_slide_people.on("click","li",function(){
        var self = $(this);
        var getOffsetLeft = $(this).offset().left;
        balancePostion = getOffsetLeft - firstItemOffsetLeft;
        getExtentChefWidth = $(this).find(".right-extent-info").outerWidth();
        /*console.log("balance:" + balancePostion);
        console.log("getExtentWidth:" + getExtentChefWidth);*/
        self.addClass("active-extend");
        sys_overview_slide_people.stop(true,false).animate({
            "left": "-=" + balancePostion
        },500,function(){
            $(this).css("width", "+=" + getExtentChefWidth);
            self.stop(true, false).animate({
                "width" : "+=" + getExtentChefWidth
            },500,function(){
                sys_btn_close_extent.show();
                sys_wrap_btn_nav.hide();
                $(this).find(".right-extent-info").fadeIn();
            });
        });
    });
    sys_btn_close_extent.on("click", function () {
        $(this).hide();
        sys_wrap_btn_nav.show();
        var liActive = sys_overview_slide_people.find(".active-extend");
        liActive.find(".right-extent-info").fadeOut(function () {
            liActive.stop(true, false).animate({
                "width": "-=" + getExtentChefWidth
            }, 500, function () {
                $(this).removeClass("active-extend");
                sys_overview_slide_people.css("width", "-=" + getExtentChefWidth).stop(true,false).animate({
                    "left": "+=" + balancePostion
                },500,function(){

                });
            });
        });
    });
    /*$("#sys_overview_slide_people").on("click","li",function(){
        $(this).parent().addClass("hideChild");
        $("#sys_wrap_btn_nav").fadeOut();
        $("#sys_btn_close_extent").fadeIn();

        var getIdx = $(this).index();
        var extentElem = $("#sys_wrap_extent_infomation").children(":eq(" + getIdx + ")");
        extentElem.show().addClass("active");
        extentElem.children(".left-extent").stop(true, false).animate({
            "left" : 0
        }, 500, function () {});
        extentElem.children(".right-info").stop(true, false).animate({
            "top" : 0
        }, 800, function () {});
    });
    $("#sys_btn_close_extent").on("click",function(){
        $("#sys_wrap_btn_nav").fadeIn();
        $("#sys_btn_close_extent").fadeOut();
        var extentElem = $("#sys_wrap_extent_infomation").children(".active");
        extentElem.children(".left-extent").stop(true, false).animate({
            "left" : "40%"
        }, 500, function () {});

        extentElem.children(".right-info").stop(true, false).animate({
            "top" : "40%"
        }, 500, function () {
            extentElem.removeClass("active").fadeOut();
            $("#sys_overview_slide_people").removeClass("hideChild");
        });
    });*/

    // Scroll den phan thuc don
    $(".sys_link_to_foodmenu").on("click",function(){
        var getId = $(this).attr("data-di");

        $('html, body').stop(true,false).animate({
            scrollTop: $("#sys_food_item_" + getId).offset().top - $("#sys_module_header").height()
         }, 1000);
    });
    /*Thực đơn mở rộng*/
    var sys_food_item = $(".sys_food_item");
    var sys_foodmenu_item = $(".sys_foodmenu_item");
    var sys_content_zoom = $(".sys_content_zoom");
    var grid_frame = $(".grid_frame").first();
    sys_food_item.each(function(idx,elem) {
        $(elem).find(".extent-menu").css({
            "width": $(window).width(),
            "left": "-" + $(elem).offset().left +"px"
        });
    });

    $(".extent-menu").on("click",function(e){
        e.stopPropagation();
    });
    sys_food_item.on("click",function(){
        if($(this).find(".extent-menu").length==0) {
            alert("Menu mở rộng không có nội dung.");
        }else{
            $(this).find(".extent-menu").on("click",function(e){
                e.stopPropagation();
            });
            //collapse other extent
            $(this).siblings(".sys_food_item").removeClass("active-extend").css("margin-bottom","0");

            if($(this).hasClass("active-extend")){
                $(this).stop(true, false).animate({
                    "margin-bottom": "0"
                },500,function(){
                    $(this).removeClass("active-extend");
                });
            }else{
                $(this).stop(true, false).animate({
                    "margin-bottom": "632px"
                },500,function(){
                    $(this).addClass("active-extend");
                    $(this).find('.sys_slider_foodmenu').first().tinycarousel({
                        duration: 500
                    });
                    //$(this).find(".extent-menu").
                    $('html, body').stop(true,false).animate({
                        scrollTop: $(this).find(".extent-menu").offset().top - $("#sys_module_header").height()
                     }, 1000);
                });
            }
        }
    });
    $(".sys_btn_close_extent").on("click", function () {
        $(this).parents(".sys_food_item").find(".sys_slider_foodmenu").css("visibility","hidden");
        $(this).parents(".sys_food_item").stop(true, false).animate({
            "margin-bottom": "0"
        },500,function(){
            $(this).removeClass("active-extend");
            $(this).find(".sys_slider_foodmenu").css("visibility","");
        });
        $('html, body').stop(true,false).animate({
            scrollTop: $(this).parents(".sys_food_item").offset().top - $("#sys_module_header").height()
         }, 1000)
    });

    /*zoom*/
    var mox_right = ($(window).width() * 2 + grid_frame.width()) / 4;
    mox_right -= 5;// 5: sai so
    sys_foodmenu_item.on("click",function(){
        //hide other zoomed
        $(".sys_content_zoom").fadeOut();

        var getOffLeft = $(this).parent().offset().left;
        var getIdx = $(this).parent().index();
        var dockRight = (getOffLeft > mox_right);
        var dockBottom = (getIdx == 2);
        $(this).find(".sys_content_zoom").css({
            "left": (dockRight) ? "auto" : 0,
            "right": (dockRight) ? 0 : "auto",
            "top": (dockBottom) ? "auto" : 0,
            "bottom": (dockBottom) ? 0 : "auto"
        }).fadeIn().on("click",function(e){
                $(this).fadeOut();
                e.stopPropagation();
            });
        $(this).find(".thumb-zoom").attr("src", $(this).find(".thumb-zoom").attr("data-src"));
    });
});